﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmLevelInfo : Form
    {
        LevelInfo _LevelInfo;
        DialogResult dlgResult;
        RoleRightInfo listRole;
        public frmLevelInfo(int LevelID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            //LoadLanguage();
            dlgResult = DialogResult.Cancel;
            _LevelInfo = new LevelInfo();

            LevelClient wcfLevel = new LevelClient(ProxyConfig.GetBinding(), ProxyConfig.Level);
            _LevelInfo.ID = LevelID;

            if (LevelID > 0)
            {
                _LevelInfo = wcfLevel.Level_GetInfo(LevelID);
                Info2Form();
            }
            listRole = _listRole;
            if (!listRole._create)
                btnNew.Enabled = false;
            if (!listRole.Update)
                btnSave.Enabled = false;
        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            label1.Text = UpdateUI.GetString(label1.Tag.ToString());
        }

        void Info2Form()//Hiển thị dữ liệu
        {
            txtName.Text = _LevelInfo.Name;
        }

        void Form2Info()//Chuyển dữ liệu vừa nhập vào info
        {
            _LevelInfo.Name = txtName.Text.Trim();
        }

        bool CheckValid()//kiểm tra dữ liệu nhập
        {
            if (txtName.Text.Trim().Length == 0)
            {
                txtName.Focus();
                Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), "Bậc không được để trống", 500, 3000, 500);
                return false;
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckValid())
            {
                LevelClient wcfLevel = new LevelClient(ProxyConfig.GetBinding(), ProxyConfig.Level);

                Form2Info();
                if (_LevelInfo.ID > 0)//update
                {
                    if (!listRole.Update)
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        return;
                    }
                    if (wcfLevel.Level_Update(_LevelInfo))
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        dlgResult = DialogResult.OK;
                    }
                    else
                        Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                }
                else//insert
                {
                    if (!listRole.Create)
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        return;
                    }
                    _LevelInfo.ID = wcfLevel.Level_Insert(_LevelInfo);
                    if (_LevelInfo.ID > 0)
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("themmoithanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        dlgResult = DialogResult.OK;
                    }
                    else
                        Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("themmoikhongthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                }
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                Program.taskbarNotifier.Show(UpdateUI.GetString("canhbao"), UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                return;
            }
            if (_LevelInfo.ID == 0)
            {
                MessageBox.Show("Bạn muốn lưu trước khi tạo mới");
                return;
            }
            _LevelInfo = new LevelInfo();
            _LevelInfo.ID = 0;
            txtName.Text = "";
            txtName.Focus();
        }

        private void frmLevelInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = dlgResult;
        }

        private void frmLevelInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
            }
        }
    }
}
